/**
 * Created by dongchen on 2016-03-29
 */
define(['ViewBase', 'jquery', 'lodash', 'ajaxUtil', 'moment'], function (ViewBase, $, _, ajaxUtil, moment) {
    var
        tplPath = '/js/module/single/single.tpl',
        url_balance = '/admin/balance',
        url_flowset = '/admin/flowset',
        url_payment = '/admin/payment';

    function buildFlowsetContent(data) {
        if (data.length > 0) {
            var flowset = data[0].subAccuInfoList;
            var total = 0;
            var result = '';
            _.each(flowset, function (monthData) {
                var period = moment(monthData.startTime, 'YYYYMMDDhhmmss').format('YYYY年MM月');
                var flowLeft = +monthData.accuAmount;
                total += flowLeft;
                var fr = flowCal(flowLeft);

                result += `${period}结余:${fr.suitable}</br>`
            });
            var tr = flowCal(total);
            result += `总计剩余流量:${tr.suitable}`;
            return result;
        } else {
            return '无记录';
        }
    }

    function flowCal(kb) {
        var mb = (kb / 1024).toFixed(2);
        var gb = (mb / 1024).toFixed(2);

        var result = {
            kb: kb + 'K',
            mb: mb + 'M',
            gb: gb + 'G'
        };
        result.suitable = gb >= 1 ? result.gb : (mb >= 1 ? result.mb : result.kb);
        return result;
    }

    var view = ViewBase.extend({
        events: {
            'click -> .demo-1-balance-submit': 'onClickBalanceQueryHandler',
            'click -> .demo-1-flowset-submit': 'onClickFlowsetQueryHandler',
            'click -> .demo-1-chargeInfo-submit': 'onClickChargeInfoeQueryHandler'
        },

        init: function (opts) {
            view.__super__.init(opts);
        },

        update: function () {
        },

        render: function () {
            this.el.html(_.template(this.getTpl(tplPath, "demo-1-tpl")));
        },

        onClickBalanceQueryHandler: function (event) {

            var
                _this = this,
                $target = $(event.target),
                mobile = $target.parent().find('.demo-1-balance-mobile').val(),
                type = $target.parent().find('.demo-1-balance-type').val(),
                $respCtn = _this.el.find('.demo-1-balance-resp'),
                data = {
                    mobile: mobile,
                    queryType: type
                };

            $respCtn.html('');
            ajaxUtil.doRequest_v2(url_balance, data).done(function (res) {

                var html = _.template(_this.getTpl(tplPath, 'demo-1-balance-resp-tpl'), {'variable': 'data'})(res.data);
                $respCtn.html(html);
            });
        },

        onClickFlowsetQueryHandler: function (event) {
            var
                _this = this,
                $target = $(event.target),
                mobile = $target.parent().find('.demo-1-flowset-mobile').val(),
                month = $target.parent().find('.demo-1-flowset-month').val(),
                $respCtn = _this.el.find('.demo-1-flowset-resp'),
                data = {
                    mobile: mobile,
                    month: month
                };

            $respCtn.html('');
            ajaxUtil.doRequest_v2(url_flowset, data).done(function (res) {
                var profile = buildFlowsetContent(res.data);

                var html = _.template(_this.getTpl(tplPath, 'demo-1-flowset-resp-tpl'), {'variable': 'data'})({profile : profile, detail : res.data});
                _this.el.find('.demo-1-flowset-resp').html(html);
            });
        },

        onClickChargeInfoeQueryHandler: function (event) {
            var
                _this = this,
                $target = $(event.target),
                mobile = $target.parent().find('.demo-1-chargeInfo-mobile').val(),
                month = $target.parent().find('.demo-1-chargeInfo-month').val(),
                $respCtn =_this.el.find('.demo-1-chargeInfo-resp'),
                data = {
                    mobile: mobile,
                    month: month
                };
            $respCtn.html('');
            ajaxUtil.doRequest_v2(url_payment, data).done(function (res) {

                var html = _.template(_this.getTpl(tplPath, 'demo-1-chargeInfo-resp-tpl'), {'variable': 'data'})(res.data);
                $respCtn.html(html);
            });
        }
    });
    return view;
});